using System;
using System.Collections.Generic;
using System.Linq;

using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    class CauTrucTmp
    {
        private TimeSpan _timeStart;
        private int _lane;

        public CauTrucTmp(TimeSpan timeStart, int lane)
        {
            _timeStart = timeStart;
            _lane = lane;
        }

        public TimeSpan TimeStart
        {
            get { return _timeStart; }
            set { _timeStart = value; }
        }

        public int Lane
        {
            get { return _lane; }
            set { _lane = value; }
        }
    }

    class AI
    {
        private GameplayScreen _owner;

        private Dictionary<string, Queue<CauTrucTmp>> _zombies = new Dictionary<string, Queue<CauTrucTmp>>();

        //private Queue<string> _zombies;

        //private Queue<TimeSpan> _timeStart;

        //private Queue<int> _lane; 

        public AI(GameplayScreen owner, string fileName)
        {
            _owner = owner;

            XDocument doc = XDocument.Load(fileName);
            XName name = XName.Get(@"Definition");
            if (doc.Document != null)
            {
                IEnumerable<XElement> definitions = doc.Document.Descendants(name);

                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"Type");
                    if (xAttribute != null)
                    {
                        string type = xAttribute.Value;
                        var property = definition.Descendants(XName.Get(@"Property"));                        
                        XName name2 = XName.Get(@"Def");
                        var def = property.Descendants(name2);
                        foreach (var xElement in def)
                        {
                            TimeSpan t = TimeSpan.Parse(xElement.Attribute(@"Time").Value);
                            int lane = Int32.Parse(xElement.Attribute(@"Lane").Value);
                            CauTrucTmp tmp = new CauTrucTmp(t, lane);
                            if (_zombies.ContainsKey(type))
                            {
                                (_zombies[type]).Enqueue(tmp);
                            }
                            else
                            {
                                Queue<CauTrucTmp> newQueue = new Queue<CauTrucTmp>();
                                newQueue.Enqueue(tmp);
                                _zombies.Add(type, newQueue);
                            }
                        }                                                
                    }
                }
                int count = 0;
                foreach (var zomby in _zombies)
                {
                    count += zomby.Value.Count;
                }
                _owner.NumZombie = count;
            }
        }

        public void Update(TimeSpan mapTime)
        {
            if (_zombies.Count <= 0)
                return;
            foreach (var zomby in _zombies)
            {
                if (zomby.Value.Count > 0)
                {
                    CauTrucTmp tmp = zomby.Value.Peek();
                    while (tmp != null && tmp.TimeStart <= mapTime)
                    {
                        zomby.Value.Dequeue();
                        string type = zomby.Key;
                        var laneIndex = tmp.Lane;
                        Blank b = LaneManager.GetLane(laneIndex).GetLastBlank();
                        switch (type)
                        {
                            case "Zero":
                                Zero z = new Zero(_owner.CurrGame, b, b.Position + new Vector2(b.Size.X - 50, 0));
                                z.Initialize();
                                b.Add(z);
                                break;
                            case "Skeleton":
                                ZSkeleton skeleton = new ZSkeleton(_owner.CurrGame, b,
                                                                   b.Position + new Vector2(b.Size.X - 50, 0));
                                skeleton.Initialize();
                                b.Add(skeleton);
                                break;
                            case "SpineSkeleton":
                                ZSpineSkeleton spineSkeleton = new ZSpineSkeleton(_owner.CurrGame, b,
                                                                                  b.Position +
                                                                                  new Vector2(b.Size.X - 50, 0));
                                spineSkeleton.Initialize();
                                b.Add(spineSkeleton);
                                break;
                            case "NZombie":
                                ZNormal nzombie = new ZNormal(_owner.CurrGame, b,
                                                              b.Position + new Vector2(b.Size.X - 50, 0));
                                nzombie.Initialize();
                                b.Add(nzombie);
                                break;
                            case "HZombie":
                                ZNormalHat hzombie = new ZNormalHat(_owner.CurrGame, b,
                                                                    b.Position + new Vector2(b.Size.X - 50, 0));
                                hzombie.Initialize();
                                b.Add(hzombie);
                                break;
                            case "Tiger":
                                ZTiger zTiger = new ZTiger(_owner.CurrGame, b,
                                                           b.Position + new Vector2(b.Size.X - 50, 0));
                                zTiger.Initialize();
                                b.Add(zTiger);
                                break;
                            case "ZMouse":
                                ZMouse zMouse = new ZMouse(_owner.CurrGame, b,
                                                           b.Position + new Vector2(b.Size.X - 50, 0));
                                zMouse.Initialize();
                                b.Add(zMouse);
                                break;
                        }
                        tmp = zomby.Value.Count > 0 ? zomby.Value.Peek() : null;
                    }
                }
            }
        }
    }
}
